<template>
  <div id="app">
    <keep-alive>
      <router-view v-if="!$route.meta.notKeepAlive" />
    </keep-alive>
    <router-view v-if="$route.meta.notKeepAlive" />
    <Tabbar v-if="isFirst" />
  </div>
</template>
<script>
import './styles/default.less';
import './styles/common.less';
import './styles/vant.less';
import './styles/icon.less';
import Tabbar from '@/components/Tabbar';
import { setToken, setKey, getKey, getToken, clearKey, clearToken } from "./utils/auth";
import { getUrlParams, getHashParams } from './utils';
import BASE_URL from './config/api';
import appInfo from './config';
import axios from 'axios';
export default {
  components: {
    Tabbar,
  },
  data() {
    return {
      isFirst: true,
      loading: false,
    }
  },
  methods: {
    wxLogin(sessionKey) {
      axios.get(`${BASE_URL}/DataServices/MPWeixin/GetSessionToken?key=${sessionKey}`).then((ret) => {
        const data = ret.data;
        if (data.ok === 1) {
          const token = data.result;
          setToken(token);
          setKey(sessionKey);
          window.location.reload();
        } else {
          alert(data.msg || '登录失败');
        }
      }).catch((e) => {
        alert(e.msg || '登录失败');
      })
    },
    getWxLogin() {
      const sessionKey = getUrlParams().sessionKey || getHashParams().sessionKey;
      const token = getToken();
      if (sessionKey) {
        if (!token) {
          this.wxLogin(sessionKey);
        } else {
          this.$http.my.checkToken().then(({ result }) => {
            if (!result) {
              this.getSession();
            }
          })
        }
        if(this.$route.query.dev === 'true'){
          window.localStorage.clear();
          clearKey();
          alert('已清除缓存');
          alert(window.location.href);
        }
      } else if (!token) {
        this.getSession();
      } else if (token) {
        this.$http.my.checkToken().then(({ result }) => {
          if (!result) {
            this.getSession();
          }
        })
      }
    },
    getSession() {
      clearKey();
      clearToken();
      const url = encodeURIComponent(window.location.href);
      window.location.href = `${BASE_URL}/DataServices/MPWeixin/OnLogin?url=${url}`;
    }
  },
  created() {
    this.isFirst = window.location.hash.split('/').length < 3;
    if (this.$route.query.dev === 'true') {
      window.localStorage.clear();
      clearKey();
      clearToken();
      if (this.$route.query.token) {
        setToken(this.$route.query.token);
      }
      if (this.$route.query.key) {
        setKey(this.$route.query.key);
      }
      window.location.href = window.location.origin + window.location.pathname;
    } else {
      this.getWxLogin();
    }
  },
  watch: {
    '$route'(to) {
      // 判断是否为二级页面
      this.isFirst = to.path.split('/').length < 3;
      document.documentElement.scrollTop = 0;
      document.body.scrollTop = 0;
    }
  }
}
</script>

<style lang="less">
.token{
  position: relative;
  z-index: 10000;
  top: 0;
  left: 0;
}
</style>
